import React, { Component } from 'react'
import ReactDOM from 'react-dom';
import { Modal, Form, message } from 'antd';
import { ModalForm } from '../../../components'
import { fetchData } from '../../../components/AjaxConsumer'
import store from '../../../stores'
const confirm = Modal.confirm;

const WrappedAdvancedSearchForm = Form.create()(ModalForm);

export default class UpdateRegionPage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: false,
            visible: false,
            resData: null,
            submitUrl:'/base/region/c'
        };
        //和stroes进行通信挂钩 并绑定解绑事件unsubscribe
        this.unsubscribe = store.listen((resData)=>{
            this.setState({
                resData: resData
            })
        })

        this.formAlerSize = 'small'; //弹出框的大小 只有'large'和'small'两种值 （必须,输错时默认为large）
        this.getFormData = [
            {
                type : 'hiddenInput',
                showName : 'regionId',
                name : 'regionId',
                initialValue : ''
            },
            {
                type : 'input',
                showName : '区域编码',
                name : 'regionCode',
                pattern : /^[-a-zA-z0-9]*$/,
                errorMessage : '输入内容不合法',
                max : 40,
                maxErrMsg :  '输入内容应少于40个字符',  
                initialValue : '',
                required:'required'  //选填
            },{
                type : 'input',
                showName : '区域描述',
                name : 'regionDesc',
                pattern : /^[a-zA-z0-9\u4E00-\u9FA5]*$/,
                errorMessage : '输入内容不合法',
                max : 600,
                maxErrMsg :  '输入内容应少于600个字符',
                initialValue : '',
                required:'required'
            },{
                type : 'select',
                showName : '仓库名称',
                name : 'whCode',
                optionList:[],
                initialValue : '',
                required:'required'
            },{
                type : 'select',
                showName : '生产工厂',
                name : 'productFactoryCode',
                optionList:[],
                initialValue : '',
                required:'required'
            },{
                type : 'select',
                showName : '激活标记',
                name : 'activeFlag',
                initialValue : '',
                required:'required',
                optionList:[
                    {
                        optionKey:'1',
                        optionShow:'启用'
                    },{
                        optionKey:'2',
                        optionShow:'禁用'
                    }
                ]
            }
        ];
    }
    //组件销毁时解绑与stores的监听
    componentWillUnmount(){
        this.unsubscribe();
    }
    //显示弹框
    showModal = () => {
        this.setState({
            visible: true,
        });
    }
    // 隐藏弹出框
    hideModal = () => {
        this.setState({
            visible: false,
        });
    }
    // 新增工厂
    addRegionHandle = () => {
        this.setState(
            {
                submitUrl:'/base/region/c',
                formTitle: '新增区域'
            }
        );
        //清空input值
        this.getFormData.map( (e, index) => {
            e.initialValue = ''
        });
        this.setState({resData: null});
        this.showModal();//显示弹框
    }

    editGetInfoCallBack =(resData)=>{
        this.setState(
            {
                submitUrl:'/base/region/u',
                resData: resData,
                formTitle: '修改区域'
            }
            );
        this.showModal();//显示弹框
    }

    // 编辑信息
    editHandle = (rowObj) => {
        this.setState({resData: null});
        //根据对象的Id 属性获取对象信息
        let regionId = rowObj.regionId;
        const options= {
            url: '/base/region/r/'+regionId,
            type: 'get',//请求方式
            condition:{}
        };
        fetchData(this, options, this.editGetInfoCallBack);
    }
    // 删除后的回调
    deleteCallBackHandle = (resData)=>{
        let result = resData;
        //alert("提交返回数据：\n",JSON.stringify(result, null, 4));
        if (result.code == '1'){
            message.success('删除成功！');
            this.refreshHandle();
        }else {
            message.info(result.msg);
        }
    }

    // 删除工厂
    deleteHandle = (rowObj) => {
        let regionId = rowObj.regionId;
        // let userIdArr = [];
        // userIdArr.push(userId);
        const options= {
            url: '/base/region/d',
            condition:{regionId:regionId}
        };
        let _that = this;
        confirm({
            title: '您确定要删除吗？',
            onOk() {
                fetchData(_that, options, _that.deleteCallBackHandle);
                console.log("------------删除操作------------------")
            },
            onCancel() {
                console.log("取消了删除操作")
            },
        });
    }

    // 新增/修改后的回调
    handleOk = () => {
        //debugger;
        //let result = this.state.resData;
        //console.log("提交返回数据：\n",JSON.stringify(result, null, 4));
        //if (result.code == "1"){
            this.hideModal();
            this.refreshHandle();
        //}else {
        //    alert(result.msg);
        //}

    }

    handleCancel = () => {
        //表单重置
        this.refs.modalFrom.resetFields();
        this.hideModal();
    }

    //刷新List列表
    refreshHandle = () => {
        console.log("刷新List列表")
        this.props.refreshHandle();
    }

    componentDidMount() {
        let factoryCode = 'all';
        const options= {
            url: '/base/data/warehouse/r/' + factoryCode + '/warehouse',
            type: 'post'
        };
        fetchData(this, options, this.dicInitCallBackHandle);

        let factoryType = '0';
        const options1= {
            url: '/base/factory/r/'+factoryType,
            type: 'post'
        };
        fetchData(this, options1, this.dicInitCallBackHandle1);
    }

    
    //下拉框 初始化
    dicInitCallBackHandle = (resData)=>{
        console.log('resData' + JSON.stringify(resData, null, 4));
        let optionList = [];
        resData.data.map( (e,index) => {
            let tmp = {};
            tmp.optionKey = e.warehouseCode;
            tmp.optionShow = e.warehouseName;
            optionList.push(tmp);
        });
        this.getFormData[3].optionList = optionList;
    }

    dicInitCallBackHandle1 = (resData)=>{
        let optionList1 = [];
        resData.data.map( (e,index) => {
            let tmp1 = {};
            tmp1.optionKey = e.factoryCode;
            tmp1.optionShow = e.factoryName;
            optionList1.push(tmp1);
        });
        this.getFormData[4].optionList = optionList1;
    }



    render() {
        if(this.state.resData != null &&  this.state.resData.data != null){
            let factory = this.state.resData.data;
            this.getFormData.map( (e, index) => {
                e.initialValue = factory[e.name]
            });
        }

        return(
            <Modal
                style={{top:'20px'}}
                visible={this.state.visible}
                title={this.state.formTitle ? this.state.formTitle : '填写表单'}
                onOk={this.handleOk}
                onCancel={this.handleCancel}
                width={this.formAlerSize=='large'?'850px':'400px'}
                footer=''
            >

                <WrappedAdvancedSearchForm
                    ref="modalFrom"
                    title="区域信息"
                    formAlerSize={this.formAlerSize}
                    formUrl={this.state.submitUrl}
                    getFormData={this.getFormData}
                    closeBox={this.handleCancel}
                    checkBox={this.handleOk} />

            </Modal>
        )
    }
}